package com.springcloud.demo.service;

import com.springcloud.demo.pojo.Order;
import com.springcloud.demo.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Author: XiaoJie Wang
 * @Description:
 * @Date: Create in 16:39 2021/7/20
 */
@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    private ProductService productService;

    /**
     * 根据主键查询订单
     *
     * @param id
     * @return
     */
    @Override
    public Order selectOrderById(Integer id) {
        return new Order(id, "order-001", "中国", 22788D,
                productService.selectProductList());
    }

    /**
     * 根据主键查询订单
     *
     * @param id
     * @return
     */
    @Override
    public Order queryOrderById(Integer id) {
        // 模拟同一时间用户发起多个请求。
        Future<Product> p1 = productService.getProductById(1);
        Future<Product> p2 = productService.getProductById(2);
        Future<Product> p3 = productService.getProductById(3);
        Future<Product> p4 = productService.getProductById(4);
        Future<Product> p5 = productService.getProductById(5);
        try {
            System.out.println(p1.get());
            System.out.println(p2.get());
            System.out.println(p3.get());
            System.out.println(p4.get());
            System.out.println(p5.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return new Order(id, "order-002", "中国", 11600D,
                null);
    }

    /**
     * 根据主键查询订单
     *
     * @param id
     * @return
     */
    @Override
    public Order searchOrderById(Integer id) {
        return new Order(id, "order-003", "中国", 2666D,
                Arrays.asList(productService.selectProductById(5)));
    }

}
